#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import os
import sys
import json
import time
import socket


if __name__ == "__main__":
    sessionId = sys.argv[1]

    data = {}
    if "WAYLAND_DISPLAY" in os.environ:
        data["WAYLAND_DISPLAY"] = os.environ["WAYLAND_DISPLAY"]
    if "DISPLAY" in os.environ:
        data["DISPLAY"] = os.environ["DISPLAY"]
    for k, v in os.environ.items():
        if k.startswith("WAYFIRE_"):
            data[k] = v

    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    while True:
        try:
            sock.connect("/run/user/%d/usrman/session-%s/retrieve-compositor-environment-variable.socket" % (os.getuid(), sessionId))
            sock.send(json.dumps(data).encode("utf-8"))
            break
        except FileNotFoundError:
            time.sleep(1)
            continue
        finally:
            sock.close()
